// 获取元素
let $ = ele => document.querySelector(ele);

let main = $('#main');
let back = $('.header-left a');
let getTop = $('.footer-top ul').children[2].children[0];

// 加载中
let span=$("header span")
// 在页面未加载完毕之前显示的loading Html自定义内容
let before = $('header span::before');
//监听加载状态改变
document.onreadystatechange = completeLoading;
//加载状态为complete时移除loading效果
function completeLoading() {
    if (document.readyState == "complete") {
        span.parentNode.removeChild(span);
    }
}

// 获取上个页面传入的productid
let val = Number(localStorage.getItem('proId'));
// 请求数据
// console.log(val);
const getProducts = () =>
    ajax({
        url: 'http://chst.vip:1234/api/getmoneyctrlproduct',
        data: { productid: val }
    });
getProducts().then((res) => {
    let html = renderProMsg(res.result[0]);
    main.innerHTML = html;
})

// 渲染商品详情
function renderProMsg(product) {
    let html = `
    <div class="productMsg">
        <h4>${product.productName}</h4>
        <p class="other">
            <span>${product.productFrom} | ${product.productTime}</span>
            <i>${product.productTips}</i>
        </p>
        <div class="msg">
        ${product.productImg2}
        ${product.productInfo2}
        </div>
        ${product.productImg2}
        ${product.productCity}
        <hr>
        <button>前往购买</button>
    </div>
    <img src="../images/mmbweixin2.png" alt="">
    ${product.productComment}`;
    return html;
}

// 返回顶部
getTop.onclick = function (e) {
    // 阻止默认事件的发生，这里阻止的a标签的跳转事件
    e.preventDefault();
    let distans = document.documentElement.scrollTop
    let distansControl = setInterval(function () {
        distans -= 10
        document.documentElement.scrollTop = distans
        if (distans <= 0) {
            clearInterval(distansControl)
        }
    }, 3)
}

// 回到上个页面
back.onclick = function (e) {
    e.preventDefault();
    history.back();
}
